package blog

import (
	"encoding/json"
	"time"
	"vblog/common"
)

//   `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '文章的Id',
//   `tags` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标签',
//   `created_at` int NOT NULL COMMENT '创建时间',
//   `published_at` int NOT NULL COMMENT '发布时间',
//   `updated_at` int NOT NULL COMMENT '更新时间',
//   `title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文章标题',
//   `author` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '作者',
//   `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文章内容',
//   `status` tinyint NOT NULL COMMENT '文章状态',
//   `summary` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文章概要信息',
//   `create_by` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '创建人',
//   `audit_at` int NOT NULL COMMENT '审核时间',
//   `is_audit_pass` tinyint NOT NULL COMMENT '是否审核通过',

type (
	BlogSet struct {
		Items []*Blog `json:"items"`
		Total int64   `json:"total"`
	}

	Blog struct {
		*common.Meta
		*common.UpdateAtRequest
		*CreateBlogRequest
		*ChangeBlogStatusRequest
	}

	CreateBlogRequest struct {
		Tags        map[string]string `json:"tags" gorm:"serializer:json"`
		Title       string            `json:"title" validate:"required,min=3" label:"标题"`
		Author      string            `json:"author" validate:"required" label:"作者"`
		Content     string            `json:"content"`
		Summary     string            `json:"summary"`
		CreateBy    string            `json:"create_by"`
		AuditAt     int64             `json:"audit_at"`
		IsAuditPass int               `json:"is_audit_pass"`
	}

	ChangeBlogStatusRequest struct {
		PublishedAt int64  `json:"published_at"`
		Status      STATUS `json:"status"`
	}
)

func NewBlogSet() *BlogSet {
	return &BlogSet{
		Items: []*Blog{},
		Total: 0,
	}
}

func NewBlog() *Blog {
	return &Blog{
		Meta:                    common.NewMeta(),
		UpdateAtRequest:         common.NewUpdateAtRequest(),
		CreateBlogRequest:       NewCreateBlogRequest(),
		ChangeBlogStatusRequest: NewChangeBlogStatusRequest(),
	}
}

func NewChangeBlogStatusRequest() *ChangeBlogStatusRequest {
	return &ChangeBlogStatusRequest{
		PublishedAt: time.Now().Unix(),
		Status:      STATUS_DRAFT,
	}

}
func NewCreateBlogRequest() *CreateBlogRequest {
	return &CreateBlogRequest{
		Tags: map[string]string{},
	}
}

func (b *Blog) Tables() string {
	return AppName
}

func (b *BlogSet) String() string {
	data, _ := json.MarshalIndent(b, " ", " ")
	return string(data)
}

func (b *Blog) String() string {
	data, _ := json.MarshalIndent(b, " ", " ")
	return string(data)
}

func (c *CreateBlogRequest) Validate() error {
	return common.Validate(c)
}
